﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using Meqs.Entity;
using Meqs.Logic.Interface;

public partial class Main : ContainerBasePage
{
    protected int CheckSchoolInfoInitState
    {
        get
        {
            bool flag = Container<ISchoolInfoLogic>.Instance.CheckSchoolInfoInitState("School");
            return flag ? 1 : 0;
        }

    }
    protected int EduType
    {
        get
        {
            if (Common.CurrentVersion == EduVersion.Education) return 1;
            else return 0;
        }
    }
    protected string GetNowYearAndTerm
    {
        get
        {
            SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
            return Common.ConvertYearAndTerm(si.SchoolYear, si.SchoolTerm);
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        _schoolName = CommonScore.SchoolName;
    }

    /// <summary>
    /// 学期升级，这里未做升级学期失败后的处理
    /// </summary>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int TermUpgrade(List<int> deptList)
    {
        IGlobalTermInfoLogic globalTermInfoLogic = Container<IGlobalTermInfoLogic>.Instance;
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());

        //查询学校信息表，获取该校学期值
        List<SchoolInfo> listSchoolInfo = (List<SchoolInfo>)schoolInfoLogic.FindAll(ht);
        if ((listSchoolInfo == null) || (listSchoolInfo.Count == 0))
            return 0;

        //查询局级学年学期表，获取全局学期值
        List<GlobalTermInfo> globalTermInfoList = (List<GlobalTermInfo>)globalTermInfoLogic.FindAll();
        if ((globalTermInfoList == null) || (globalTermInfoList.Count == 0))
            return 0;

        //如果学校学期与全局学期不一致
        if ((listSchoolInfo[0].SchoolYear != globalTermInfoList[0].GlobalYear) ||
             (listSchoolInfo[0].SchoolTerm != (byte)globalTermInfoList[0].GlobalTerm))
        {
            //如果全局学期为上学期，则需要学校学年升级
            if (globalTermInfoList[0].GlobalTerm == 1)
            {
                //调用学年升级处理过程
                YearUpgrade(deptList, Common.GetConstSchoolMark());
            }

            //学校同步学期设置信息
            listSchoolInfo[0].SchoolYear = globalTermInfoList[0].GlobalYear;
            listSchoolInfo[0].SchoolTerm = (byte)globalTermInfoList[0].GlobalTerm;
            schoolInfoLogic.Update(listSchoolInfo[0]);

            //升级成功
            return 1;
        }

        //无需升级
        return 2;
    }

    /// <summary>
    /// 验证学年学期是否与教育局同步
    /// </summary>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int ValidYearTeam()
    {
        if (Common.GetUserInfo().UserType == 3)
        {
            IGlobalTermInfoLogic globalTermInfoLogic = Container<IGlobalTermInfoLogic>.Instance;
            ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;
            Hashtable ht = new Hashtable();
            ht.Add("SchoolMark", Common.GetConstSchoolMark());

            //查询学校信息表，获取该校学期值

            List<SchoolInfo> listSchoolInfo = (List<SchoolInfo>)schoolInfoLogic.FindAll(ht);
            if ((listSchoolInfo == null) || (listSchoolInfo.Count == 0))
                return 0;

            //查询局级学年学期表，获取全局学期值

            List<GlobalTermInfo> globalTermInfoList = (List<GlobalTermInfo>)globalTermInfoLogic.FindAll();
            if ((globalTermInfoList == null) || (globalTermInfoList.Count == 0))
                return 0;

            //如果学校学期与全局学期不一致,小于全局学期，则进行学校的学年升级
            int iYear = globalTermInfoList[0].GlobalYear - listSchoolInfo[0].SchoolYear;

            if (iYear > 0) return 1;    //要升级学年
            else if (iYear == 0)
            {
                //如果学年相等，学期不等
                if (listSchoolInfo[0].SchoolTerm != globalTermInfoList[0].GlobalTerm)
                    return 2;           //要升级学期
            }

            return 0;
        }
        return 0;

    }
    /// <summary>
    /// 学校学年升级
    /// </summary>
    /// <param name="schoolMark"></param>
    /// <returns></returns>
    public static int YearUpgrade(List<int> deptList, string schoolMark)
    {
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", schoolMark);
        //选择部分学部升级
        ht.Add("GSCHOOL", deptList[0]);
        ht.Add("JHSCHOOL", deptList[1]);
        ht.Add("SHSCHOOL", deptList[2]);
        schoolInfoLogic.YearUpgrade(ht);

        return 1;
    }

    /// <summary>
    /// 修改密码
    /// </summary>
    /// <param name="schoolMark"></param>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static int UpdatePwd(string oldPwd, string newPwd)
    {
        //局级

        if (Common.GetUserInfo().UserType == 1)
        {
            IEducationUsersLogic eduUserLogic = Container<IEducationUsersLogic>.Instance;

            Hashtable ht = new Hashtable();
            ht.Add("RID", Common.GetUserInfo().UID);
            ht.Add("Passwd", oldPwd);
            int iCount = eduUserLogic.FindCount(ht);

            if (iCount == 0) return -1;          //原密码不正确

            //重置密码
            List<int> list = new List<int>();
            list.Add(Common.GetUserInfo().UID);
            return eduUserLogic.ResetPwd(newPwd, list);
        }

        //校级
        if (Common.GetUserInfo().UserType == 3)
        {
            ITeacherInfoLogic teacherLogic = Container<ITeacherInfoLogic>.Instance;

            Hashtable ht = new Hashtable();
            ht.Add("RID", Common.GetUserInfo().UID);
            ht.Add("Passwd", oldPwd);
            int iCount = teacherLogic.FindCount(ht);

            if (iCount == 0) return -1;          //原密码不正确

            //重置密码
            List<int> list = new List<int>();
            list.Add(Common.GetUserInfo().UID);
            return teacherLogic.ResetPassword(newPwd, list);
        }

        return 0;

    }

    /// <summary>
    /// 获取当前登录学校信息
    /// </summary>
    /// <returns></returns>
    [System.Web.Services.WebMethod]
    public static SchoolInfo GetSchoolInfo()
    {
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;

        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", Common.GetConstSchoolMark());
        ht.Add("IsEffect", 1);
        List<SchoolInfo> schoolInfoList = (List<SchoolInfo>)schoolInfoLogic.FindAll(ht);

        if (schoolInfoList.Count > 0)
            return schoolInfoList[0];
        else
            return null;
    }

    /// <summary>
    /// 保存学校信息
    /// </summary>
    /// <param name="schoolName"></param>
    /// <param name="year"></param>
    /// <param name="term"></param>
    /// <param name="opt"></param>
    [System.Web.Services.WebMethod]
    public static void SaveSchoolInfo(string schoolName, int year, int term, int opt, String startDate, String endDate)
    {
        ISchoolInfoLogic schoolInfoLogic = Container<ISchoolInfoLogic>.Instance;
        schoolInfoLogic.UpdateSchool(schoolName, year, term, opt, SchoolMark, startDate, endDate);

        // 更新Session
        Perm perm = HttpContext.Current.Session["UserInfo"] as Perm;
        if (perm == null) return;
        perm.SchoolName = schoolName;
        HttpContext.Current.Session["UserInfo"] = perm;
        _schoolName = schoolName;
    }

    private static string _schoolName;
    public string SchoolName
    {
        get { return _schoolName; }
    }
}